﻿program Диспетчер;

uses
  Forms,
  MainFrm in 'MainFrm.pas' {MainForm},
  scin in 'scin.pas',
  Unit1 in 'Unit1.pas' {Form1},
  common in 'common.pas',
  DModule in 'common\DModule.pas' {dm1: TDataModule},
  GetUserFrm in 'GetUserFrm.pas' {GetUserForm},
  NewReestrFrm in 'NewReestrFrm.pas' {NewReestrForm},
  GetSprForm in 'common\GetSprForm.pas',
  ProgresFrm in 'common\ProgresFrm.pas' {ProgresForm},
  adCpuUsage in 'common\adCpuUsage.pas',
  NewPrincipFrm in 'NewPrincipFrm.pas' {NewPrincipForm},
  cmUtils in 'common\cmUtils.pas',
  SrvSaveListFrm in 'SrvSaveListFrm.pas' {SrvSaveListForm},
  SrvFltUnit in 'SrvFltUnit.pas',
  NameFrm in 'NameFrm.pas' {NameForm},
  CollectorRepFrm in 'CollectorRepFrm.pas' {CollectorRepForm},
  SchetFrm in 'SchetFrm.pas' {SchetForm},
  VolSchetFrm in 'VolSchetFrm.pas' {VolSchetForm},
  DataSetEvents in 'common\DataSetEvents.pas' {EVM: TDataModule},
  Common_sql in 'common\Common_sql.pas',
  EventRepFrm in 'EventRepFrm.pas' {EventRepForm},
  EventGrRepFrm in 'EventGrRepFrm.pas' {EventGrRepForm},
  UserFrm in 'UserFrm.pas' {UserForm},
  AddrFrm in 'AddrFrm.pas' {AddrForm},
  ETypeFrm in 'ETypeFrm.pas' {ETypeForm},
  SetGridEh in 'common\SetGridEh.pas',
  SysUtils,
  Windows,
  IniFiles,
  ShellAPI,
  ETypeEditFrm in 'ETypeEditFrm.pas' {ETypeEditForm},
  UpgradeFrm in 'UpgradeFrm.pas' {UpgradeForm},
  ChangeCollectorFrm in 'ChangeCollectorFrm.pas' {ChangeCollectorForm},
  NewReport in 'NewReport.pas' {NewRepForm},
  NewRepParamFrm in 'NewRepParamFrm.pas' {NewRepParamForm},
  AgentRepFrm in 'AgentRepFrm.pas' {AgentRepForm},
  ScanEventFrm in 'ScanEventFrm.pas' {ScanEventForm},
  MoveCollector in 'MoveCollector.pas' {MoveCollectorForm},
  NewScanEventFrm in 'NewScanEventFrm.pas' {NewScanEventForm},
  SetENoteFrm in 'SetENoteFrm.pas' {SetENoteForm},
  VirsionFrm in 'VirsionFrm.pas' {VersionForm},
  NewMPFrm in 'NewMPFrm.pas' {NewMPForm},
  GetMListFrm in 'GetMListFrm.pas' {GetMListForm},
  GetRListFrm in 'GetRListFrm.pas' {GetRListForm},
  SetPhoneFrm in 'SetPhoneFrm.pas' {SetPhoneForm},
  DistrictFrm in 'DistrictFrm.pas' {DistrictForm},
  LofinFrm in 'login\LofinFrm.pas' {LoginForm},
  UpdateA in 'update\UpdateA.pas',
  UpdScrFrm in 'update\UpdScrFrm.pas' {UpdScrForm},
  cmLoadDataSet in 'common\cmLoadDataSet.pas',
  cmFileUtil in 'common\cmFileUtil.pas',
  cmXML in 'common\cmXML.pas';

{$R *.res}
  var
    Update : TUpdateApp;
    Ini: TIniFile;

    path,curVers,newVers,uptool :string;
    upgrade :Boolean;
begin
  Update:=TUpdateApp.Create;
  with Update do
  begin
    Ini:=TIniFile.Create(ExtractFilePath(Application.ExeName)+'config\conf.ini');
    UpdateType:=fileUpdate;
    AppExe:=ExtractFileName(Application.ExeName);
    ToolExe:=ExtractFilePath(Application.ExeName)+'Update.exe';
    UpdPath:=ini.ReadString('Update','UpdPath','');
    AppPath:=ExtractFilePath(Application.ExeName);
    NewFileName:=ini.ReadString('Update','NewFileName','Upgrade_Disp.rar');
  end;
  if not Update.DoUpdate('conf.ini') then
  begin
      Application.Initialize;
      Application.MainFormOnTaskbar := True;
      Application.Title := 'Модуль "Диспетчер"';
      Application.CreateForm(TMainForm, MainForm);
  Application.CreateForm(TEVM, EVM);
  Application.CreateForm(Tdm1, dm1);
  Application.CreateForm(TUpdScrForm, UpdScrForm);
  LoginForm:=TLoginForm.Create(Application);
      LoginForm.ShowModal;
      if LoginForm.ModalResult=1 then
      begin
        LoginForm.Free;
        Update.Free;
        Ini.Free;
        Application.Run;
      end else
      begin
       LoginForm.Free;
       Update.Free;
       Ini.Free;
       Application.Run;
      end;
  end else
  begin
   Update.Free;
   Ini.Free;
   Application.Run;
  end;

  {Application.Initialize;
  upgrade:=False;
  if FileExists(ExtractFilePath(Application.ExeName)+'forms.ini') then
  begin
   ini:=TIniFile.Create(ExtractFilePath(Application.ExeName)+'forms.ini');
   if ini.SectionExists('AppVersion') then
   begin
     path:=ini.ReadString('AppVersion','path','');
     if path<>'' then
     begin
      curVers:=GetFileVersionEx(Application.ExeName);
      newVers:=GetFileVersionEx(path+ExtractFileName(Application.ExeName));
      if (newVers<>'') and (curVers<>newVers) then
      begin
        //
        UpgradeForm:=TUpgradeForm.Create(Application);
        UpgradeForm.ShowModal;
        if UpgradeForm.ModalResult=1 then
        begin
         // Exit;
          upgrade:=True;
          uptool:=ExtractFilePath(Application.ExeName)+'Upgrade.exe';
          ShellExecute(Application.Handle, nil, PChar(uptool),nil,
                                                         nil, SW_RESTORE);
        end;
        UpgradeForm.Free;
      end;
     end;
   end;
   ini.Free;
  end;
  if upgrade=True then Exit;
  Application.MainFormOnTaskbar := True;
        Application.Title := 'Модуль "Диспетчер"';
  Application.CreateForm(Tdm1, dm1);
  GetUserForm := TGetUserForm.Create(Application);
  if dm1.db1.Connected=true then
  begin
    GetUserForm.ShowModal;
    if GetUserForm.ModalResult=1   then
    begin
       GetUserForm.Close;
       GetUserForm.Free;
       Application.CreateForm(TMainForm, MainForm);
       Application.CreateForm(TEVM, EVM);
       Application.Run;
    end else
    begin
       GetUserForm.Close;
       GetUserForm.Free;
       dm1.Free;
       MainForm.Free;
    end;
  end;}
  //Application.Run;



end.
